var movingText = "Left click to measure distance.<br/>Right click to end measurement";
var clearMeasureTitle = "Clear measurement";
function beginMeasure(){
	
    if(currentMap.mapProvider == 'geoserver'){
    	currentMap.measureControls.line.deactivate();
    	var removes = [];
    	for(var i=0; i < currentMap.geoMarkers.markers.length; i++){
    		var m = currentMap.geoMarkers.markers[i];
    		if(m.markerType ==  'measureLabel'){
    			removes.push(m);
    		}
    	}
    	for(var i=0; i < removes.length; i++){
    		var m = removes[i];
    		currentMap.geoMarkers.removeMarker(m);
    	}
    	
    	currentMap.geoMarkers.setZIndex(350);
    	currentMap.measureControls.line.activate();
    	currentMap.measureControls.line.events.on({
            "measure": handleEndMeasurements,
            "measurepartial": handleMeasurements
        });
    	return;
    }

	try{
		isDragged = true;
		//g_seluin = "";
	    //update_sel_uin(true);
	}catch(e){}
	

	
	var markers = [];
	var labels = [];
	var lines = [];
	var previousPoint = null;
	var totalDistance = 0;
	
	function setLabel(pos){
        if(previousPoint){
        	if(labels[labels.length - 1]){
        		labels[labels.length - 1].put_text(" <span style='background-color:white;color:black;left:5px;position:absolute;'>"+Math.round(totalDistance*100)/100+" KM</span>");
        	}
        	var g_label = new GPS_Label(pos, currentMap.map);
        	currentMap.addLabel(g_label);
        	var meters = pos.distanceFrom(previousPoint);
        	totalDistance += meters;
        	g_label.put_text(" <span style='background-color:white;color:black;left:10px;top:10px;position:absolute;'> Total "+Math.round(totalDistance*100)/100+" KM</span>");
        	labels.push(g_label);
        	
            var g_polyline = new GPS_Polyline([previousPoint, pos], "#FF0000", 3);
             currentMap.addPolyline(g_polyline);
             lines.push(g_polyline);
        }
	}
	
	//var movingMarker = new GPS_Marker(new GPS_LatLng(0,0), {draggable: true});
	//currentMap.addOverlay(movingMarker); <img src='images/rule5.png' style='width:80px;height:25px;' />
	var movingLabel = new GPS_Label(new GPS_LatLng(0,0), currentMap.map);
	currentMap.addLabel(movingLabel);
	movingLabel.put_text("<span style='background-color:white;color:black;left:10px;top:10px;position:absolute;height:35px;'>" + movingText+" </span>");
	
	var movingPoint = null;
	
	var clkListen = currentMap.addListener("click", function(context, pos){
		var m = new GPS_Marker(pos, {draggable: false}, currentMap.map);
		currentMap.addOverlay(m);
		markers.push(m);
			setLabel(pos);
	        previousPoint = pos;
	});
	
	var move = currentMap.addListener("mousemove", function(context, pos){
		//document.getElementById('realtimePanel').style.cursor="url('http://api.map.baidu.com/images/ruler.cur')";
		//currentMap.setDraggableCursor("url('http://api.map.baidu.com/images/ruler.cur')");
		//currentMap.setDraggingCursor('move');
		var s=event.srcElement;
		if(s)
			{
			s.parentElement.style.cursor="url('images/ruler.cur')";
			}
		movingLabel.move(pos);
		movingPoint = pos;
		//movingMarker.setPoint(pos);
	});
	
	
	var rightClk = currentMap.addListener("rightclick", function(map, params){
		//var point = params[0];
		var pos = movingPoint;//currentMap.fromDivPixelToLatLng(point);
		pos = new GPS_LatLng(pos.lat(), pos.lng());
		currentMap.removeListener(null,clkListen);
		currentMap.removeListener(null,move);
		currentMap.removeListener(null,rightClk);
		//currentMap.removeOverlay(movingMarker);
		setLabel(pos);
		
		movingLabel.remove();
		
		var ic = new GPS_Icon();
		ic.iconSize = new GPS_Size(13, 12);
		ic.shadow = "";
		ic.image = "images/clear.jpg";
        var options = {icon: ic, title:clearMeasureTitle}; 
		var m = new GPS_Marker(pos, options, currentMap.map);
		//currentMap.addOverlay(m);
		m.addListener("click", function(){
			currentMap.removeOverlay(m);
			
			for(var i = 0; i < markers.length; i++){
				currentMap.removeOverlay(markers[i]);
			}
			for(var i = 0; i < lines.length; i++){
				currentMap.removePolyline(lines[i]);
			}
			
			for(var j = 0; j < labels.length; j++){
				labels[j].remove();
			}
			
			
		});
	});
	
}